﻿
Public Class snake


    Dim random As New Random()
    Private snakeDirection As Point = New Point(1, 0) ' 初始方向向右
    Private foodLocation As Point
    Private snakeBody As New List(Of Point) From {New Point(5, 5)}
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        foodLocation = New Point(5, 3)
        Timer1.Start()
        Panel1.TabIndex = 0
        Panel1.Select()
    End Sub


    Private Sub Panel1_Paint_1(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
        ' 绘制蛇
        For Each part In snakeBody
            e.Graphics.FillRectangle(Brushes.Black, part.X * 10, part.Y * 10, 10, 10)
        Next

        ' 绘制食物
        e.Graphics.FillEllipse(Brushes.Red, foodLocation.X * 10, foodLocation.Y * 10, 10, 10)
    End Sub
    Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
        ' 移动蛇
        Dim head As Point = snakeBody.Last
        Dim newHead As Point = New Point(head.X + snakeDirection.X, head.Y + snakeDirection.Y)
        snakeBody.Add(newHead)
        Label2.Text = newHead.X
        Label3.Text = newHead.Y
        ' 检测是否吃到食物
        If newHead = foodLocation Then
            ' 随机生成新食物位置
            foodLocation = New Point(random.Next(1, 20), random.Next(1, 15))
        Else
            snakeBody.RemoveAt(0)
        End If

        If newHead.X = 0 Then
            snakeDirection = New Point(0, -1)
        End If
        If newHead.Y = 0 Then
            snakeDirection = New Point(1, 0)
        End If

        If newHead.X = 63 Then
            snakeDirection = New Point(0, 1)
        End If

        If newHead.Y = 32 Then
            snakeDirection = New Point(-1, 0)
        End If

        If newHead.Y = 32 And newHead.X = 0 Then
            snakeDirection = New Point(0, -1)
        End If
        ' 绘制
        Refresh()

    End Sub
    ' 按键处理（简单移动）
    Private Sub Panel1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Panel1.PreviewKeyDown
        Select Case e.KeyCode
            Case Keys.W
                If snakeDirection.Y <> 1 Then snakeDirection = New Point(0, -1)
            Case Keys.S
                If snakeDirection.Y <> -1 Then snakeDirection = New Point(0, 1)
            Case Keys.A
                If snakeDirection.X <> 1 Then snakeDirection = New Point(-1, 0)
            Case Keys.D
                If snakeDirection.X <> -1 Then snakeDirection = New Point(1, 0)

         '处理键盘的上下左右键
            Case Keys.Up
                If snakeDirection.Y <> 1 Then snakeDirection = New Point(0, -1)
            Case Keys.Down
                If snakeDirection.Y <> -1 Then snakeDirection = New Point(0, 1)
            Case Keys.Left
                If snakeDirection.X <> 1 Then snakeDirection = New Point(-1, 0)
            Case Keys.Right
                If snakeDirection.X <> -1 Then snakeDirection = New Point(1, 0)
        End Select

    End Sub



    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Timer1.Enabled = False

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Timer1.Enabled = True

    End Sub

    Private Sub Button1_Enter(sender As Object, e As EventArgs) Handles Button1.Enter
        Panel1.TabIndex = 0
        Panel1.Select()

    End Sub

    Private Sub Button2_Enter(sender As Object, e As EventArgs)
        Panel1.TabIndex = 0
        Panel1.Select()
    End Sub

    Private Sub Button3_Enter(sender As Object, e As EventArgs) Handles Button3.Enter
        Panel1.TabIndex = 0
        Panel1.Select()
    End Sub

    Private Sub Button4_Enter(sender As Object, e As EventArgs) Handles Button4.Enter
        Panel1.TabIndex = 0
        Panel1.Select()
    End Sub

    Private Sub Button1_KeyDown(sender As Object, e As KeyEventArgs) Handles Button1.KeyDown
        Select Case e.KeyCode
            Case Keys.Down, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Up, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Left, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Right, Keys.Space, Keys.Tab
                e.Handled = True
        End Select
    End Sub

    Private Sub Button2_KeyDown(sender As Object, e As KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.Down, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Up, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Left, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Right, Keys.Space, Keys.Tab
                e.Handled = True
        End Select
    End Sub

    Private Sub Button3_KeyDown(sender As Object, e As KeyEventArgs) Handles Button3.KeyDown
        Select Case e.KeyCode
            Case Keys.Down, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Up, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Left, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Right, Keys.Space, Keys.Tab
                e.Handled = True
        End Select
    End Sub

    Private Sub Button4_KeyDown(sender As Object, e As KeyEventArgs) Handles Button4.KeyDown
        Select Case e.KeyCode
            Case Keys.Down, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Up, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Left, Keys.Space, Keys.Tab
                e.Handled = True
            Case Keys.Right, Keys.Space, Keys.Tab
                e.Handled = True
        End Select
    End Sub
End Class


